<template>
  <div class="knowledge-settings">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>知识库设置</span>
          <el-button type="primary" @click="handleAddKnowledge">添加知识库</el-button>
        </div>
      </template>
      <el-table :data="knowledgeBases" style="width: 100%" empty-text="暂无数据">
        <el-table-column prop="name" label="知识库名称" />
        <el-table-column prop="type" label="类型" />
        <el-table-column prop="description" label="描述" show-overflow-tooltip />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'active' ? 'success' : 'info'">
              {{ scope.row.status === 'active' ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="50%">
      <el-form :model="knowledgeForm" label-width="120px">
        <el-form-item label="知识库名称">
          <el-input v-model="knowledgeForm.name" />
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="knowledgeForm.type" placeholder="请选择类型">
            <el-option label="文档" value="document" />
            <el-option label="网页" value="webpage" />
            <el-option label="数据库" value="database" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="knowledgeForm.description" type="textarea" :rows="3" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="knowledgeForm.status" :active-value="'active'" :inactive-value="'inactive'" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue';
import { useKnowledgeStore } from '@/store/knowledge';
import type { Knowledge } from '@/store/knowledge';

export default defineComponent({
  name: 'KnowledgeSettings',
  setup() {
    const knowledgeStore = useKnowledgeStore();
    const dialogVisible = ref(false);
    const dialogTitle = ref('添加知识库');
    const knowledgeBases = ref<Knowledge[]>([]);

    const knowledgeForm = reactive({
      id: '',
      name: '',
      type: '',
      description: '',
      status: 'active' as 'active' | 'inactive'
    });

    const handleAddKnowledge = () => {
      dialogTitle.value = '添加知识库';
      knowledgeForm.id = '';
      knowledgeForm.name = '';
      knowledgeForm.type = '';
      knowledgeForm.description = '';
      knowledgeForm.status = 'active';
      dialogVisible.value = true;
    };

    const handleEdit = (row: any) => {
      dialogTitle.value = '编辑知识库';
      Object.assign(knowledgeForm, row);
      dialogVisible.value = true;
    };

    const handleDelete = async (row: any) => {
      await knowledgeStore.deleteKnowledge(row.id);
      await fetchKnowledgeBases();
    };

    const handleSave = async () => {
      if (knowledgeForm.id) {
        await knowledgeStore.updateKnowledge(knowledgeForm);
      } else {
        await knowledgeStore.createKnowledge(knowledgeForm);
      }
      dialogVisible.value = false;
      await fetchKnowledgeBases();
    };

    const fetchKnowledgeBases = async () => {
      knowledgeBases.value = await knowledgeStore.fetchKnowledgeBases();
    };

    fetchKnowledgeBases();

    return {
      dialogVisible,
      dialogTitle,
      knowledgeBases,
      knowledgeForm,
      handleAddKnowledge,
      handleEdit,
      handleDelete,
      handleSave
    };
  }
});
</script>

<style lang="scss" scoped>
.knowledge-settings {
  padding: 20px;

  .box-card {
    margin-bottom: 20px;
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style> 